﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableFrameAttachmentRetriever
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApCableFrameAttachmentRetriever : ICApCableFrameRegistry
{
  private Dictionary<ITxLocatableObject, TxRobot> m_frameToRobotDictionary;

  public CApCableFrameAttachmentRetriever()
  {
    this.m_frameToRobotDictionary = new Dictionary<ITxLocatableObject, TxRobot>();
  }

  public void RetrieveAttachingRobot(ITxLocatableObject frame, out TxRobot robot)
  {
    robot = (TxRobot) null;
    if (this.m_frameToRobotDictionary.ContainsKey(frame))
    {
      robot = this.m_frameToRobotDictionary[frame];
    }
    else
    {
      this.DoRetrieveAttachingRobot(frame, out robot);
      this.m_frameToRobotDictionary[frame] = robot;
    }
  }

  public void RetrieveAttachingRobot(ITxLocatableObject frame, ITxObject obj, out TxRobot robot)
  {
    robot = (TxRobot) null;
    if (this.m_frameToRobotDictionary.ContainsKey(frame))
    {
      robot = this.m_frameToRobotDictionary[frame];
    }
    else
    {
      this.DoRetrieveAttachingRobot(obj as ITxLocatableObject, out robot);
      this.m_frameToRobotDictionary[frame] = robot;
    }
  }

  public void UpdateFrameAttachmentRegistry(ITxLocatableObject frame)
  {
    if (frame == null)
      return;
    TxRobot robot = (TxRobot) null;
    if (this.m_frameToRobotDictionary.ContainsKey(frame))
    {
      if (CApCableUtility.IsAttached(frame, (ITxObjectCollection) this.m_frameToRobotDictionary[frame]))
        return;
      this.m_frameToRobotDictionary.Remove(frame);
    }
    else
    {
      this.DoRetrieveAttachingRobot(frame, out robot);
      this.m_frameToRobotDictionary[frame] = robot;
    }
  }

  public void UpdateFrameAttachmentRegistry(
    ITxLocatableObject frame,
    ITxLocatableObject attachedObject)
  {
    if (frame == null)
      return;
    TxRobot robot = (TxRobot) null;
    if (this.m_frameToRobotDictionary.ContainsKey(frame))
    {
      if (CApCableUtility.IsAttached(attachedObject, (ITxObjectCollection) this.m_frameToRobotDictionary[frame]))
        return;
      this.m_frameToRobotDictionary.Remove(frame);
    }
    else
    {
      this.DoRetrieveAttachingRobot(attachedObject, out robot);
      this.m_frameToRobotDictionary[frame] = robot;
    }
  }

  private void DoRetrieveAttachingRobot(ITxLocatableObject obj, out TxRobot robot)
  {
    robot = (TxRobot) null;
    if (obj == null)
      return;
    TxObjectList allDescendants = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxRobot)));
    foreach (ITxObject objParent in (Collection<ITxObject>) allDescendants)
    {
      if (CApCableUtility.IsAttached(obj, objParent as ITxObjectCollection))
      {
        robot = objParent as TxRobot;
        break;
      }
    }
    if (!TxRobot.op_Equality(robot, (ITxObject) null))
      return;
    ITxGun objParent1 = (ITxGun) null;
    foreach (ITxRobot itxRobot in (Collection<ITxObject>) allDescendants)
    {
      for (int index = 0; index < ((Collection<ITxObject>) itxRobot.MountedTools).Count; ++index)
      {
        if (((Collection<ITxObject>) itxRobot.MountedTools)[index] is ITxGun)
        {
          objParent1 = ((Collection<ITxObject>) itxRobot.MountedTools)[index] as ITxGun;
          break;
        }
      }
      if (objParent1 != null && CApCableUtility.IsAttached(obj, objParent1 as ITxObjectCollection))
      {
        robot = itxRobot as TxRobot;
        break;
      }
    }
  }
}
